layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 系统管理--im用户管理
     */
    var ImUser = {
        tableId: "imuserTable",    //表格id
        condition: {
            name: ""
        }
    };

    /**
     * 初始化表格的列
     */
    ImUser.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'imUserId'},
            {field: 'name', align: "center", sort: true, title: '姓名'},
            {field: 'account', align: "center", sort: true, title: '账号'},
            {field: 'password', align: "center", sort: true, title: '密码'},
            {field: 'age', align: "center", sort: true, title: '年龄'},
            {field: 'gender', align: "center", sort: true, title: '性别'},
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ImUser.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        table.reload(ImUser.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 弹出添加imuser
     */
    ImUser.openAddImuser = function () {
        func.open({
            height: 600,
            title: '添加IM用户',
            content: Feng.ctxPath + '/imuser/imuser_add',
            tableId: ImUser.tableId
        });
    };

    /**
     * 点击编辑imuser
     *
     * @param data 点击按钮时候的行数据
     */
    ImUser.onEditImuser = function (data) {
        func.open({
            height: 600,
            title: '修改IM用户',
            content: Feng.ctxPath + "/imuser/imuser_edit?imUserId=" + data.imUserId,
            tableId: ImUser.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    ImUser.exportExcel = function () {
        var checkRows = table.checkStatus(ImUser.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除IM用户
     *
     * @param data 点击按钮时候的行数据
     */
    ImUser.onDeleteImuser = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/imuser/remove", function () {
                Feng.success("删除成功!");
                table.reload(ImUser.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("imUserId", data.imUserId);
            ajax.start();
        };
        Feng.confirm("是否删除IM用户 " + data.name + "?", operation);
    };

    /**
     * 分配菜单
     *
     * @param data 点击按钮时候的行数据
     */
    ImUser.imuserAssign = function (data) {
        parent.layer.open({
            type: 2,
            title: '权限配置',
            area: ['300px', '450px'], //宽高
            fix: false,
            maxmin: true,
            content: Feng.ctxPath + '/imuser/imuser_assign/' + data.imUserId,
            end: function () {
                table.reload(ImUser.tableId);
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ImUser.tableId,
        url: Feng.ctxPath + '/imuser/list',
        page: true,
        height: "full-98",
        cellMinWidth: 100,
        cols: ImUser.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ImUser.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ImUser.openAddImuser();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ImUser.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ImUser.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ImUser.onEditImuser(data);
        } else if (layEvent === 'delete') {
            ImUser.onDeleteImuser(data);
        } else if (layEvent === 'imuserAssign') {
            ImUser.imuserAssign(data);
        }
    });
});
